
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()

	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS ) 
	self.Die = false
	
	self.StartSound = Sound("weapons/smokegrenade/sg_explode.wav")
	self.Boom = Sound("weapons/flashbang/flashbang_explode1.wav")
	
	local phys = self:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
		phys:SetMass(100)
	end
	
end

function ENT:ExtExplode(owner,pos,damage,radius)

	local tb = ents.FindInSphere(pos,radius) or {}
	
	for k,v in pairs(tb) do
		v:TakeDamage(damage,owner)
		if v:IsPlayer() then
			v:DoExtinguish()
		end
		if string.find(v:GetClass(),"fire") then
			v:Remove()
		end
	end
end

function ENT:Explode()
	
	local effectdata = EffectData()
	effectdata:SetOrigin(self:GetPos())
	util.Effect("extinguish_explode", effectdata)
	
	self:ExtExplode(self.Owner,self:GetPos(),1,300)
	self:EmitSound(self.Boom,80,math.random(90,110))
	
	self:Remove()

end

function ENT:SetPwner(ply)
	self.Pwner = ply
end

/*---------------------------------------------------------
   Name: Think
---------------------------------------------------------*/
function ENT:Think()
	if not self.Spray then
		self.Spray = true
		local effectdata = EffectData()
		effectdata:SetEntity(self)
		util.Effect("extinguish_spray", effectdata)
	end
	if self.Exploded then
		self:Explode()
	end
end 
 
 /*---------------------------------------------------------
   Name: takedamage
---------------------------------------------------------*/
function ENT:OnTakeDamage( dmginfo )	
	if self.Exploded then return end
	self.Exploded = true
end


/*---------------------------------------------------------
   Name: PhysicsCollide
---------------------------------------------------------*/
function ENT:PhysicsCollide( data, physobj )
	if data.Speed > 50 and data.DeltaTime > 0.2  then
		self:EmitSound(ChooseRandom(GameSounds.MetalHit),80,math.random(90,110))
		if not self.Spray then
			self.Spray = true
			self:EmitSound(self.StartSound,80,math.random(90,110))
		end
	end
end